﻿var SelectareSubtipConcediu = function(pageContainer) {

    SelectareSubtipConcediu.superclass.constructor.call(this, pageContainer);

    var self = this;
    var subtipuriConcedii = new Array();
    var currentTipConcediu = null;
    var currentCallback = null;

    this.CanGoBack = false;

    this.isPopup = true;

    var Elements = {
        list: pageContainer.find(".list"),
        loader: pageContainer.find(".loadMore")

    };

    var Templates = {
        subtipConcediuTemplate: pageContainer.find('.subtipConcediuTemplate'),
        subtipConcediuTemplateCompiled: null
    };

    this.refresh = function(tipConcediu, callback) {
        Elements.loader.show();
        currentTipConcediu = tipConcediu;
        currentCallback = callback;
        app.translatePage(app.Pages.SelectareSubtipConcediu);

        pageContainer.bind('onBeforePageShow', function() {
            app.Phone.setBackbtnVisible(true);
        });

        app.TMMService.Cereri.GetSubtipuriConcedii(tipConcediu.ELM_ID, tipConcediu.TCO_ID, function(response) {
            if (response.Exception)
                Elements.loader.hide();
            else {
                subtipuriConcedii = response.FinalResult;
                constructList(response.FinalResult);
            }
        });
    };

    var constructList = function(data) {
        if (!data)
            data = new Array();

        if (!Templates.subtipConcediuTemplateCompiled) {
            var source = Templates.subtipConcediuTemplate.html();
            Templates.subtipConcediuTemplateCompiled = Handlebars.compile(source);
        }

        var html = "";
        for (var i = 0; i < data.length; i++)
            html += Templates.subtipConcediuTemplateCompiled(data[i]);
        Elements.list.html(html);
        Elements.loader.hide();

        if (currentCallback) {
            Elements.list.find('.list-element').click(function() {
                var index = Elements.list.children().index($(this).parent());
                currentCallback({
                    Tip: currentTipConcediu,
                    Subtip: subtipuriConcedii[index]
                });
            });
        }
    };

    self.initialize = override(self.initialize, function() {
        self.Title = translate('Module_Title_SelectareSubtipConcediu');
    });

    this.setModuleAction = override(self.setModuleAction, function() {
        if (app.Phone)
            app.Phone.setModuleAction(ModuleActions.None);
    });

    self.initialize();
};
extend(SelectareSubtipConcediu, TMMobilePage);